/**
 * 
 */
package org.youisoft.summer.evaluator.operator.base;
import org.youisoft.summer.evaluator.InvokeException;
import org.youisoft.summer.evaluator.context.CalContext;
import org.youisoft.summer.evaluator.function.Function;
import org.youisoft.summer.evaluator.operator.AbstractOperator;
import org.youisoft.summer.evaluator.operator.Operator;
import org.youisoft.summer.evaluator.registry.FunctionRegistry;

/**
 * @author zijiang.jl
 *
 */
public class ClosureOperator extends AbstractOperator<String>{


	private String name;
	
	private FunctionRegistry registry;
	
	
	public ClosureOperator(){
		
	}
	
	public ClosureOperator(String expression){
		this.setValue(expression);
	}
	
	
	
	
	public final String getName() {
		return name;
	}

	public final ClosureOperator setName(String name) {
		this.name = name;
		return this;
	}

	public <C, R, P> Operator<?> execute(CalContext<C, R, P> context) throws InvokeException{
		Function fun=this.registry.findFunction(this.name);
		if(fun!=null&&!fun.isIgnore(context)&&fun.supportOperator(context, this)){
			return fun.invoke(context, this);
		}
		return null;
	}

	public final ClosureOperator setRegistry(FunctionRegistry registry) {
		this.registry = registry;
		return this;
	}
	
	
}
